package lance.honeydew.dao;

import java.util.List;

import lance.honeydew.bean.CategoryBig;
import lance.honeydew.bean.PaymentMethod;
import lance.honeydew.bean.TransportMethod;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

public class TransportMethodDAO extends RootDAO {
	
	private static final Logger logger = Logger.getLogger(TransportMethodDAO.class);
	
	/**
	 * findTransportMethodBySeqId
	 * @param seqId
	 * @return
	 */
	public TransportMethod findTransportMethodBySeqId(Long seqId) {
		
		TransportMethod result = null;
		
		try {
			
			DetachedCriteria criteria = DetachedCriteria.forClass(TransportMethod.class);
			criteria.add(Restrictions.eq(TransportMethod.PROP_SEQID, seqId));
			
			criteria.setProjection(Projections.projectionList()
					.add(Projections.property(TransportMethod.PROP_SEQID), "_seqid")
					.add(Projections.property(TransportMethod.PROP_TRANSPORTNAME), "_transportname")
					.add(Projections.property(TransportMethod.PROP_TRANSPORTPRICE), "_transportprice")
					);
			criteria.setResultTransformer(Transformers.aliasToBean(TransportMethod.class));
			
			result = (TransportMethod) this.getHibernateTemplate().
					findByCriteria(criteria).get(0);
			
			
			result = (TransportMethod) this.findById(TransportMethod.class, PaymentMethod.PROP_SEQID, seqId);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * queryTransportMethodByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<TransportMethod> queryTransportMethodByQb(TransportMethod qb, int pageNum, int pageSize) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(TransportMethod.class);
		List<TransportMethod> list = null;
		
		try {
			
			if (StringUtils.isNotEmpty(qb.getTransportname())) {
				criteria.add(Restrictions.like(TransportMethod.PROP_TRANSPORTNAME, qb.getTransportname(), MatchMode.ANYWHERE));
			}
			
			if (qb.getTransportprice() != null) {
				criteria.add(Restrictions.eq(TransportMethod.PROP_TRANSPORTPRICE, qb.getTransportprice()));
			}
			
			list = (List<TransportMethod>) this.getHibernateTemplate().
					findByCriteria(criteria);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return list;
	}
	
	/**
	 * findAllTransportMethodList
	 * @return
	 */
	public List<TransportMethod> findAllTransportMethodList() {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(TransportMethod.class);
		List<TransportMethod> list = null;
		
		try {
			
			criteria.setProjection(Projections.projectionList()
					.add(Projections.property(TransportMethod.PROP_SEQID), "_seqid")
					.add(Projections.property(TransportMethod.PROP_TRANSPORTNAME), "_transportname")
					.add(Projections.property(TransportMethod.PROP_TRANSPORTPRICE), "_transportprice"));
			criteria.setResultTransformer(Transformers.aliasToBean(TransportMethod.class));
			
			list = (List<TransportMethod> ) this.getHibernateTemplate().
					findByCriteria(criteria);
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return list;
	}
}